use std::env;
use resource::resource_manager_client::ResourceManagerClient;
use resource::ResourceRequest;

pub mod resource {
    tonic::include_proto!("resource");
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<String> = env::args().collect();

    if args.len() < 4 {
        eprintln!("用法: {} <allocate|free> <client_id> <resource_id1> [resource_id2 ...]", args[0]);
        std::process::exit(1);
    }

    let command = &args[1];
    let client_id = &args[2];
    let resource_ids = args[3..].to_vec();

    let mut client = ResourceManagerClient::connect("http://[::1]:50051").await?;

    let request = tonic::Request::new(ResourceRequest {
        client_id: client_id.clone(),
        resource_ids,
    });

    let response = match command.as_str() {
        "allocate" => client.allocate(request).await?,
        "free" => client.free(request).await?,
        _ => {
            eprintln!("无效命令: {}", command);
            std::process::exit(1);
        }
    };

    println!("[客户端] 服务端响应: {}", response.get_ref().message);

    Ok(())
}
